#include "Utility.h"

#include <iostream>
#include <limits> 
#include <climits> 

unsigned int BitUtility::alignGreaterEqual(unsigned int v, unsigned int base) {
    if (v > base) {
        return ((v & (v-1)) == 0)?  v : 1 << bitWidth(v);
    } else {
        return base;
    }
}

unsigned int BitUtility::bitWidth(unsigned int value) {
    if (value == 0) return 1; 

    unsigned int count = 0;
    while (value) { 
        ++count;
        value >>= 1;
    }
    return count;
}

static unsigned long long MASK = 0xFFFFFFFFFFFFFFFF; 

Bit::Bit(unsigned int bindex) {
    _bindex = (bindex >= 64)? 63 : bindex;
}

unsigned int Bit::bindex() { return _bindex; }
unsigned long long Bit::value() { return 1 << _bindex; }
unsigned long long Bit::mask() { return (1 << (_bindex + 1)) - 1; } 
unsigned long long Bit::clear(unsigned long long v) { return v & (~(1 << _bindex) & MASK); }
unsigned long long Bit::set(unsigned long long v) { return v | (1 << _bindex); }
bool Bit::isSet(unsigned long long v) { return ((1 << _bindex) & v) != 0; }
bool Bit::isClear(unsigned long long v) { return ((1 << _bindex) & v) == 0; }
    
